# coding=utf-8
import requests
import json


class tieba():
    def __init__(self):
        self.headers = {
            "Cookie": "BDUSS=JuOGV0cFhndHRmQkhQdnVlUEttSmNWYTdOdldLcWRTSUJYMWZaS1NZRm1od05jQVFBQUFBJCQAAAAAAAAAAAEAAACfPOcCZGVzcXExMTEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGb621tm-ttbaT;",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/14B100 UCBrowser/10.7.5.650 Mobile"
        }
        self.qianurl = 'https://tieba.baidu.com/sign/add'
        self.tbs=None
        self.lbdata=None
        self.ret = []


    def getlb(self):
        url = 'https://tieba.baidu.com/mo/q/newmoindex?bdid=A0E98DA424865E55A193B5AB93091482:FG%3D1'
        res = requests.get(url=url, headers=self.headers)
        res = json.loads(res.content.decode('utf-8'))
        self.tbs = res['data']['tbs']
        self.lbdata = res['data']['like_forum']


    def qian(self,bm):

        headers_qd = self.headers
        headers_qd['Content-Type'] = "application/x-www-form-urlencoded"
        data = {
            'ie': "utf-8",
            'kw': bm,
            'tbs': self.tbs,
        }
        res = requests.post(url=self.qianurl,data=data,headers=headers_qd)
        con = json.loads(res.content.decode('utf-8'))
        if con['no'] == 0:
            return '签到成功'
        else:return con['error']

    def run(self):
        self.getlb()
        self.ret=[]
        for i in self.lbdata:
            res = self.qian(i['forum_name'])
            self.ret.append(i['forum_name']+':'+res)
